<template>
  <div v-loading="loading" class="device-list-main">
    <!-- filter start -->
    <el-form ref="filterForm" inline :model="filterForm" label-width="120px" size="small">
      <el-form-item
        prop="name"
        label="终端名称："
      >
        <el-input
          v-model="filterForm.name"
          placeholder="请输入"
          clearable
          class="app-w-350"
          @keyup.enter.native="submitFilterForm"
        />
      </el-form-item>
      <el-form-item
        prop="kdDeviceNo"
        label="终端编号："
      >
        <el-input
          v-model="filterForm.kdDeviceNo"
          placeholder="请输入"
          clearable
          class="app-w-350"
          @keyup.enter.native="submitFilterForm"
        />
      </el-form-item>
      <el-form-item
        prop="outDeviceNo"
        label="第三方编号："
      >
        <el-input
          v-model="filterForm.outDeviceNo"
          placeholder="请输入"
          clearable
          class="app-w-350"
          @keyup.enter.native="submitFilterForm"
        />
      </el-form-item>
      <el-form-item
        prop="shopId"
        label="店铺："
      >
        <el-select v-model="filterForm.shopId" filterable clearable placeholder="可通过名称搜索" class="app-w-350">
          <el-option label="全部店铺" value />
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <div class="flex-x-between-center">
        <el-form-item label=" ">
          <el-button type="primary" round @click="submitFilterForm">查询终端</el-button>
          <el-button round @click="resetFilterForm">重置条件</el-button>
          <el-button type="primary" size="small" round style="margin-bottom: 18px" @click="handleDeviceAuthCode">设备授权码
          </el-button>
        </el-form-item>
      </div>
    </el-form>

    <!--  列表  -->
    <div v-if="!loading&&list.length===0" class="empty-tips">
      查询暂无数据
    </div>
    <el-row v-else :gutter="16" type="flex">
      <el-col v-for="item in list" :key="item.kdDeviceNo" :md="12" :lg="8" :xl="6">
        <el-card :body-style="{ padding: '20px 15px'}">
          <div class="name app-ellipsis-l1">
            {{ item.deviceName }}
          </div>
          <div class="more-info">
            <div class="app-ellipsis-l1">编号：{{ item.kdDeviceNo || '-' }}</div>
            <div class="app-ellipsis-l1">店铺：{{ item.shopName || '-' }}</div>
            <div class="app-ellipsis-l1">第三方编号：{{ item.outDeviceNo || '-' }}</div>
          </div>
          <div class="status-info">
            <span v-if="item.deviceStatusInfoVo.bindStatus===1" class="app-color-primary">已绑定</span>
            <span v-else>未绑定</span>
            <span
              v-if="item.deviceStatusInfoVo.loginStatus===1"
              class="app-color-primary"
            >，{{ item.deviceStatusInfoVo.loginUsername }}登录使用中</span>
            <span v-else>，未登录</span>
          </div>
          <div class="handle-group">
            <el-button type="text" size="mini" icon="el-icon-edit" @click.native="handleEdit(item)"> 编辑
            </el-button>
            <el-button v-if="item.deviceStatusInfoVo.bindStatus===1" type="text" size="mini" icon="el-icon-remove-outline" class="app-color-danger" @click.native="handleUnBind(item)">
              解绑
            </el-button>
            <el-button v-if="item.deviceStatusInfoVo.loginStatus===1" type="text" size="mini" icon="el-icon-remove-outline" class="app-color-danger" @click.native="handleLogout(item)">
              退出
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!--分页-->
    <PaginationDirection
      v-if="paginationConfig.total!==0"
      :config.sync="paginationConfig"
      @sizesChange="fetchData"
      @pageChange="fetchData"
    />

    <!--组件-->
    <MchDeviceFormDialog ref="mchDeviceFormDialog" />
    <DeviceActiveCodeDialog ref="deviceActiveCodeDialog" />

  </div>
</template>

<script>
import MchDeviceFormDialog from './MchDeviceFormDialog'
import DeviceActiveCodeDialog from './DeviceActiveCodeDialog'
import { deviceList, unbindDevice, logoutDevice } from '@/api/mch/equipment'
import { allShopList } from '@/api/mch/shop'

export default {
  components: {
    MchDeviceFormDialog,
    DeviceActiveCodeDialog
  },
  data() {
    return {
      // 配置
      // 分页配置
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      // 状态
      loading: false,
      // 数据
      shopOptions: [],
      filterForm: {
        businessId: null,
        name: '',
        kdDeviceNo: '',
        outDeviceNo: '',
        shopId: null,
        pageNum: 1,
        pageSize: 10
      },
      list: []
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
    // 定义全局刷新方法
    this.$root.$on('refreshMchEquipmentDeviceList', () => this.fetchData())
  },
  beforeMount() {
  },
  mounted() {
    this.getAllShopOptions()
    this.fetchData()
  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
    this.$root.$off('refreshMchEquipmentDeviceList')
  },
  destroyed() {
  },
  methods: {
    /**
     * 数据组
     */
    // 获取列表
    fetchData(
      page = this.paginationConfig.page,
      pageSize = this.paginationConfig.pageSize
    ) {
      // 请求参数
      const params = Object.assign({}, this.filterForm)
      params.pageNum = page
      params.pageSize = pageSize
      params.businessId = this.$route.query.mchId
      // 清除多余字段
      for (const key in params) {
        if (this.$isNull(params[key])) delete params[key]
      }
      // 请求
      this.loading = true
      console.log(params)
      deviceList({ data: params })
        .then(res => {
          this.list = res.data.list || []
          this.paginationConfig.total = res.data.total
          this.paginationConfig.page = res.data.pageNum
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 获取所有门店
    getAllShopOptions() {
      const params = {
        businessId: this.$route.query.mchId
      }
      this.loading = true
      allShopList({ params }).then(res => {
        this.shopOptions = res.data || []
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 按钮组
     */
    // 查询
    submitFilterForm() {
      this.fetchData(1)
    },

    // 重置
    resetFilterForm() {
      this.$refs.filterForm.resetFields()
    },

    // 设备授权码
    handleDeviceAuthCode() {
      this.$refs.deviceActiveCodeDialog.open()
    },

    // 编辑
    handleEdit(row) {
      this.$refs.mchDeviceFormDialog.open({ mode: 'edit', row }).then(() => {
        this.fetchData()
      })
    },

    // 解绑
    handleUnBind(row) {
      this.$confirm(`确定解绑终端 ：${row.deviceName}?`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        roundButton: true
      }).then(() => {
        this.loading = true
        const data = {
          businessId: row.businessId,
          deviceNo: row.kdDeviceNo
        }
        unbindDevice({ data }).then(() => {
          row.deviceStatusInfoVo.bindStatus = 0
          row.deviceStatusInfoVo.loginStatus = 0
          this.$notify({
            title: '成功',
            message: '解绑成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
        }).finally(() => {
          this.loading = false
        })
      })
    },

    // 退出登录
    handleLogout(row) {
      this.$confirm(`确定退出登录 ：${row.deviceName}?`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        roundButton: true
      }).then(() => {
        this.loading = true
        const data = {
          businessId: row.businessId,
          deviceNo: row.kdDeviceNo
        }
        logoutDevice({ data }).then(() => {
          row.deviceStatusInfoVo.loginStatus = 0
          this.$notify({
            title: '成功',
            message: '退出登录成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
        }).finally(() => {
          this.loading = false
        })
      })
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.device-list-main {
  padding-left: 20px;
  /*reset*/
  .el-row {
    flex-wrap: wrap;

    .el-col {
      margin-bottom: 16px;
      height: 100%;
    }
  }

  /*卡片*/
  .el-card {
    position: relative;
    border-radius: 20px;

    .handle-group {
      position: absolute;
      right: 10px;
      top: 5px;
      //display: none;
    }

    //:hover .handle-group {
    //  display: block;
    //}

    /*title*/
    .title {
      height: 50px;
      line-height: 50px;
      font-size: 18px;
      font-weight: bold;
    }

    /*card*/
    .name {
      font-size: 18px;
      padding-top: 10px;
    }

    .more-info {
      font-size: 14px;
      color: #909399;

      div {
        margin-top: 10px;
      }
    }

    .status-info {
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid #f5f5f5;
      color: #909399;
    }
  }

  /*空提示*/
  .empty-tips {
    text-align: center;
    font-size: 12px;
    padding-top: 100px;
  }
}

</style>
